package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.pojo.specification.TbSpecification;
import com.dongdongshop.pojo.specificationOption.TbSpecificationOption;
import com.dongdongshop.service.specificationOption.SpecificationOptionService;
import com.dongdongshop.service.specification.SpecificationService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Eayon
 * @Date 2020/2/21 18:37
 */
@Service
@Component
public class SpecificationServiceImpl implements SpecificationService {

    @Resource
    private TbSpecificationMapper specificationMapper;

    @Resource
    private SpecificationOptionService specificationOptionService;

    /**
     * 查询所有规格信息  不分页
     * @return
     */
    @Override
    public List<TbSpecification> getSpecificationInfo() {
        return specificationMapper.selectByExample(null);
    }

    /**
     * 添加规格和规格选项
     * @param s
     * @param specificationOptions
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSpecificationAndOption(TbSpecification s, List<TbSpecificationOption> specificationOptions) {
       //添加并返回主键id
        specificationMapper.addSpecificationAndBackId(s);
        System.out.println(JSON.toJSONString(s));
        //遍历规格选项并set规格id
        List<TbSpecificationOption> list = new ArrayList<>();
        for (TbSpecificationOption so : specificationOptions) {
            TbSpecificationOption ss = new TbSpecificationOption();
            ss.setSpecId(s.getId());
            ss.setOptionName(so.getOptionName());
            ss.setOrders(so.getOrders());
            list.add(ss);
        }
        //批量添加规格选项
        specificationOptionService.addSpecificationOptionBatch(list);
    }

    /**
     * 根据规格id查询规格信息
     * @param id
     * @return
     */
    @Override
    public TbSpecification getSpecificationById(Long id) {
        return specificationMapper.selectByPrimaryKey(id);
    }

    /**
     *
     * @param s
     * @param specificationOptions
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSpecification(TbSpecification s, List<TbSpecificationOption> specificationOptions) {
        //修改规格信息
        specificationMapper.updateByPrimaryKeySelective(s);
        //根据规格id修改规格选项信息
        //遍历规格选项并set规格id
        List<TbSpecificationOption> list = new ArrayList<>();
        for (TbSpecificationOption so : specificationOptions) {
            TbSpecificationOption ss = new TbSpecificationOption();
            ss.setSpecId(s.getId());
            ss.setOptionName(so.getOptionName());
            ss.setOrders(so.getOrders());
            list.add(ss);
        }
        specificationOptionService.updateSpecificationOptionBySpecificationId(list,s.getId());
    }


    /**
     * 根据规格id删除规格信息以及对应的规格选项信息方法
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSpecificationById(Long id) {
        //根据规格id删除规格信息
        specificationMapper.deleteByPrimaryKey(id);
        //根据规格id删除规格选项信息
        specificationOptionService.deleteSpecificationOptionBySpecificationId(id);
    }
}
